<?php


namespace app\admin\model;


use app\common\model\BaseModel;

class ImportLog extends BaseModel {

    /**
     * 列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        $count = db('import_log')
            ->alias('il')
            ->leftJoin('admin a', 'a.id=il.user_id')
            ->where('city_code',$params['city_code'])
            ->count();
        if ($count == 0) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
            return $result;
        } else {
            $data = db('import_log')
                ->alias('il')
                ->leftJoin('admin a', 'a.id=il.user_id')
                ->field('il.id,il.name,a.nickname,il.total_num,il.itime,il.status,il.err_num')
                ->order('itime desc')
                ->where('city_code',$params['city_code'])
                ->page($params['page'], $params['long'])
                ->select();
            foreach ($data as $k => $v) {
                if ($v['status'] == 1) {//失败
                    $data[$k]['num'] = $v['total_num'] . '/' . $v['err_num'];
                } else {
                    $data[$k]['num'] = $v['total_num'];
                }
                if ($v['status'] == 0) {
                    $data[$k]['status'] = '处理中';
                } elseif ($v['status'] == 1) {
                    $data[$k]['status'] = '失败';
                } else {
                    $data[$k]['status'] = '成功';
                }
                unset($data[$k]['total_num']);
                unset($data[$k]['err_num']);
                $data[$k]['itime'] = date('Y-m-d H:i:s', $data[$k]['itime']);
            }
            $pageNum = ceil($count / $params['long']);
            $result = [
                'page'    => (int)$params['page'],
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
            return $result;
        }
    }

}